/*
@title: dropper
@description: "Dropper" is an action-packed simulation puzzle game where players must navigate through gaps and dodge obstacles to reach a gold block. The objective is to carefully move left or right to avoid black boxes while progressing through various levels.
@author: hinoob
@tags: ['action','simulation','puzzle']
@addedOn: 2024-08-18
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

*/

const playera = "1"
const playerb = "2"
const playerc = "3"
const playerd = "4"
const object = "a"
const goal = "g"
setLegend(
  [ playera, bitmap`
....000000000000
....000000000000
....000003333300
....000003333300
....000003333300
....000003333300
....000003333300
....000000000000
....000000000000
....000000000000
....000000000000
....000000000000
....000000000000
....000000000000
....000000000000
....000000000000`],
  [ playerb, bitmap`
00000000000.....
00000000000.....
00333330000.....
00333330000.....
00333330000.....
00333330000.....
00333330000.....
00000000000.....
00000000000.....
00000000000.....
00000000000.....
00000000000.....
00000000000.....
00000000000.....
00000000000.....
00000000000.....`],
  [ playerc, bitmap`
.............000
.............000
.............000
.............000
.............000
.............000
.............000
.............000
.............000
.............000
.............000
.............000
5550000000000000
5550000000000000
5550000000000000
5550000000000...`],
  [ playerd, bitmap`
000.............
000.............
000.............
000.............
000.............
000.............
000.............
000.............
000.............
000.............
000.............
000.............
0000000000000555
0000000000000555
0000000000000555
...0000000000555`],
  [ object,  bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [ goal, bitmap`
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666`]
  
)

setSolids([object])

let level = 0
const levels = [
  map`
....12..................................
....34..................................
........................................
aaa...aaaaaaa....aaaaaaa.....aaaaaa..aaa
........................................
........................................
........................................
........................................
aaaaaa...aaaaaaaaa....aaaaaaaaaa..aaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
aaaaa..aaaaaa..aaaaaaaa.....aaaaaaaa..aa
........................................
........................................
........................................
........................................
........................................
........................................
........................................
aaaaa..aaaaaaa..aaaaaaa..aaaaaaa..aaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
aaaaaaaaaaaaaaaaagggaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaagggaaaaaaaaaaaaaaaaaaaa`,
  map`
.........................12.............
.........................34.............
........................................
aaaaaaaa....aaaaaaaaaa.....aaaaaaaaaaaaa
........................................
........................................
........................................
........................................
aaaaaaaaaaaaaaaa......aaaaaaaaaaaaaaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
aaa..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
aaaggggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaggggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
  map`
.....12.................................
.....34.................................
........................................
........................................
aaaaaaa..aaaaaaaaaa..aaaaaaaaaa..aaaaaaa
........................................
........................................
........................................
........................................
........................................
aaa..aaaaaaaaaaaaaaaaaaaaaaa..aaaaaaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
aaaaaaaaaaa..aaaaaaaaaaaaa..aaaaaaaaaaaa
........................................
........................................
........................................
........................................
........................................
aaaaaa..aaaaaa..aaaaaaa..aaaaaa..aaaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
aaaggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaggaaa
aaaggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaggaaa`,
  map`
.....12.................................
.....34.................................
........................................
........................................
........................................
........................................
........................................
........................................
aaaaaaaaaaaaaaa.....aaaaaaaaaaaaaaaaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
aaaaaaaaaaaaaaaaaaaaaaaaaaaa..aaaaaaaaaa
........................................
........................................
........................................
........................................
........................................
........................................
........................................
aaaaa..aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
..........a.............................
..........a.............................
aaaaaaaagga.............................`
]

setMap(levels[level])

setPushables({
  [ playera ]: [],
  [ playerb ]: [],
  [ playerc ]: [],
  [ playerd ]: []
})

onInput("d", () => {
  movePlayer(1, 0, true)
})
onInput("a", () => {
  movePlayer(-1, 0, true)
})
onInput("j", () => {
  if(gameWon) {
    gameWon = false;
    level = 0;
  }
  stopGame()
  startGame()
})

afterInput(() => {
  
})

const sleep = ms => new Promise(r => setTimeout(r, ms));
var gameLoopInterval = null;
var gameWon = false; // Whether they can retry from the start

function movePlayer(x, y, relative) {
  const playerTop = [playera, playerb];
  const playerBottom = [playerc, playerd];
  [playerTop, playerBottom].forEach(players => {
    const referenceSprite = getFirst(players[0]);

    // lock x from start
    if(referenceSprite.x == 0 && relative && x < 0) {
      return;
    }

    // lock x from end
    //console.log(referenceSprite.x + 1)
    if(referenceSprite.x + 1 == 39 && relative && x > 0) {
      return;
    }
    
    players.forEach(playerSprite => {
      const sprite = getFirst(playerSprite);

      let newX = relative ? sprite.x + x : referenceSprite.x + (sprite.x - referenceSprite.x);
      let newY = relative ? sprite.y + y : referenceSprite.y + (sprite.y - referenceSprite.y);
      sprite.x = Math.max(0, Math.min(width() - 1, newX));
      sprite.y = Math.max(0, Math.min(height() - 1, newY));
    });
  });
}

function startGame() {
  clearText()
  setMap(levels[level])
  movePlayer(Math.floor(Math.random() * 40), 0, false)
  gameLoopInterval = setInterval(() => {
    gameLoop()
  }, 550);
}

function stopGame() {
  clearInterval(gameLoopInterval)
} 

function gameLoop() {
  movePlayer(0, 1, true);
  
  const playerTopSprites = [playera, playerb];
  const playerBottomSprites = [playerc, playerd];

  // collision with objects
  const collisionDetected = playerBottomSprites.some(playerSprite => {
    const player = getFirst(playerSprite);
    const tiles = getTile(player.x, player.y);
    
    return tiles.some(tile => tile.type === object);
  });

  if (collisionDetected) {
    addText("You", { y: 2, color: "3" });
    addText("Died!", { y: 5, color: "3" });
    clearInterval(gameLoopInterval);
  } else {
    // win detection perhaps
    const win = playerBottomSprites.some(playerSprite => {
      const player = getFirst(playerSprite);
      const tiles = getTile(player.x, player.y);
    
      return tiles.some(tile => tile.type === goal);
    });

    if(win) {
      level += 1;
      
      if (level === levels.length) {
        addText("You win!", { y: 2, color: "4" });
        gameWon = true;
        stopGame();
      } else {
        setMap(levels[level]);
      }
    }
  }
}

startGame()
